var express = require('express');
var router = express.Router();
var http = require('http');

router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

//轮播图
router.get('/lunbo',function(req,res){
    //在这里我们用nodejs的http模块去访问卖座网的接口
    var t = req.query.time;
    var url = 'http://m.maizuo.com/v4/api/billboard/home?__t='+t;
    http.get(url,function(resa){
        var data = '';
        resa.on('data',function(chunk){
            data+=chunk;
        })
        resa.on('end',function(){
            res.send(data);
        })
    })
})

//正在热映
router.get('/reying',function(req,res){
    //在这里我们用nodejs的http模块去访问卖座网的接口
    var t = req.query.time;
    var url = 'http://m.maizuo.com/v4/api/film/now-playing?__page=1&count=5&t='+t;
    http.get(url,function(resa){
        var data = '';
        resa.on('data',function(chunk){
            data+=chunk;
        })
        resa.on('end',function(){
            res.send(data);
        })
    })
})


//即将上映
router.get('/jj',function(req,res){
    //在这里我们用nodejs的http模块去访问卖座网的接口
    var t = req.query.time;
    var url = 'http://m.maizuo.com/v4/api/film/coming-soon?__page=1&count=3&t='+t;
    http.get(url,function(resa){
        var data = '';
        resa.on('data',function(chunk){
            data+=chunk;
        })
        resa.on('end',function(){
            res.send(data);
        })
    })
})


//影片列表！
router.get('/yingpian',function(req,res){
    //在这里我们用nodejs的http模块去访问卖座网的接口
    // var t = req.query.time;
    var url = 'http://m.maizuo.com/v4/api/film/now-playing?page=1&count=200';
    http.get(url,function(resa){
        var data = '';
        resa.on('data',function(chunk){
            data+=chunk;
        })
        resa.on('end',function(){
            res.send(data);
        })
    })
})

// 单个影片的详情信息！

// localhost:3000/3663?time=1495796342841
router.get(/\d$/,function(req,res){
    //在这里我们用nodejs的http模块去访问卖座网的接口
     var t = req.query.time;
     console.log('??????????????????????????????');
     console.log(req.url);
    var url = 'http://m.maizuo.com/v4/api/film/3664?__t='+t;
    http.get(url,function(resa){
        var data = '';
        resa.on('data',function(chunk){
            data+=chunk;
        })
        resa.on('end',function(){
            res.send(data);
        })
    })
})













/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
